package com.dmgameclient.common;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.dmgameclient.beans.Article;
import com.dmgameclient.beans.Comment;
import com.dmgameclient.beans.Game;

public class JsonUtils {

	private static final String TAG = "JsonUtils";

	/**
	 * 解析文章列表和详细数据（标题、日期、评论数、缩略图url）
	 * 
	 * @param src
	 * @return List<Article>
	 */
	public static List<Article> parseArticleList(String src) {

		List<Article> list = new ArrayList<Article>();
		try {
			if (src != null) {
				JSONObject srcObject = new JSONObject(src);
				
				if (!srcObject.isNull("data")) {
					
					JSONObject dataObject = srcObject.getJSONObject("data");
					for (int i = 0; i < 10; i++) {
						JSONObject object = dataObject.getJSONObject(i + "");
						Article article = new Article();
						article.setId(object.getString("id"));
						article.setTypeid(object.getString("typeid"));
						article.setTitle(object.getString("title"));
						article.setLitpic(object.getString("litpic"));
						article.setPubdate(object.getString("pubdate"));
						article.setFeedback(object.getString("feedback"));
						article.setArcurl(object.getString("arcurl"));
						list.add(article);
					}
					// Log.i(TAG, ">>>List<Article>列表解析完毕大小为" + list.size());
					return list;
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解析评论列表
	 * 
	 * @param src
	 * @return List<Comment>
	 */
	public static List<Comment> parseCommentList(String src) {
		List<Comment> list = new ArrayList<Comment>();
		try {
			JSONArray dataArray = new JSONObject(src).getJSONObject(
					"description").getJSONArray("data");
			if (dataArray.length() != 0) {
				for (int i = 0; i < dataArray.length(); i++) {
					JSONObject object = dataArray.getJSONObject(i);
					Comment comment = new Comment();
					comment.setId(object.getString("id"));
					comment.setAid(object.getString("aid"));
					comment.setUsername(object.getString("username"));
					comment.setFloor(object.getString("floor"));
					comment.setMsg(object.getString("msg"));
					list.add(comment);
				}
			}
			Log.i(TAG, ">>>List<Comment>列表解析完毕大小为" + list.size());
			return list;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解析游戏列表和游戏详情
	 * 
	 * @param src
	 * @return List<Comment>
	 */
	public static List<Game> parseGameList(String src) {
		List<Game> list = new ArrayList<Game>();
		try {
			JSONObject dataObject = new JSONObject(src).getJSONObject("data");
			for (int i = 0; i < 10; i++) {
				JSONObject object = dataObject.getJSONObject(i + "");
				Game game = new Game();
				game.setId(object.getString("id"));
				game.setTypeid(object.getString("typeid"));
				game.setTitle(object.getString("title"));
				game.setLitpic(object.getString("litpic"));
				game.setPubdate(object.getString("pubdate"));
				game.setDescription(object.getString("description"));
				game.setTypename(object.getString("typename"));
				game.setWebsit(object.getString("websit"));
				game.setRelease_company(object.getString("release_company"));
				game.setMade_company(object.getString("made_company"));
				game.setTerrace(object.getString("terrace"));
				game.setRelease_date(object.getString("release_date"));
				game.setArcurl(object.getString("arcurl"));
				list.add(game);
			}
			Log.i(TAG, ">>>List<Game>列表解析完毕大小为" + list.size());
			return list;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
